package com.chhuang.ewcrm.controller;

import com.chhuang.ewcrm.consts.EwcrmConstants;
import com.chhuang.ewcrm.entity.SysUser;
import com.chhuang.ewcrm.service.SysUserService;
import com.chhuang.ewcrm.string.Md5Util;
import com.chhuang.ewcrm.vo.ResultBean;
import com.google.code.kaptcha.Constants;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * 用户登录控制类
 * @author chhuang
 * @create 2020-09-28-16:21
 */
@RestController
@RequestMapping("service")
public class LoginController {
    Logger logger= LoggerFactory.getLogger(SysUserController.class);

    /**
     * 服务对象
     */
    @Resource
    private SysUserService sysUserService;

    @PostMapping("login")
    public ResultBean<SysUser> login(String username, String password, String verificationCode, HttpServletRequest request, HttpSession session){
        ResultBean<SysUser> resultBean = new ResultBean<>();
        resultBean.setSuccess(true);
        if(StringUtils.isNotEmpty(username) && StringUtils.isNotEmpty(password) && StringUtils.isNotEmpty(verificationCode)){
            if(session.getAttribute(Constants.KAPTCHA_SESSION_KEY)!=null && verificationCode.equalsIgnoreCase(session.getAttribute(Constants.KAPTCHA_SESSION_KEY).toString())){
                SysUser user = new SysUser();
                user.setUsername(username);
                List<SysUser> users = sysUserService.query(user);
                if(users!=null && !users.isEmpty()){//用户名登录
                    user = users.get(0);
                }else {
                    user.setUsername(null);
                    user.setUserphone(username);
                    users = sysUserService.query(user);
                    if(users!=null && !users.isEmpty()){//手机号登录
                        user = users.get(0);
                    }else {
                        user.setUserphone(null);
                        user.setEmail(username);
                        users = sysUserService.query(user);
                        if(users!=null && !users.isEmpty()){//邮箱号登录
                            user = users.get(0);
                        }else {
                            resultBean.setSuccess(false);
                            resultBean.setMessage("用户名不存在");
                        }
                    }
                }
                if(resultBean.isSuccess()){
                    if(Md5Util.md5(password, Md5Util.md5(user.getUsername())).equals(user.getPassword())){
                        user.setLastLoginDevice("BROWSER");
                        user.setLastLoginIp(request.getRemoteAddr());
                        Date now = new Date();
                        user.setLastLoginTime(now);
                        user.setModifyTime(now);
                        resultBean.setData(sysUserService.update(user));
                        user.setPassword(null);
                        session.setAttribute(EwcrmConstants.SessionConst.SYS_USER, user);
                    }else {
                        resultBean.setSuccess(false);
                        resultBean.setMessage("密码错误");
                    }
                }
            }else {
                resultBean.setSuccess(false);
                resultBean.setMessage("验证码有误");
            }
        }else{
            resultBean.setSuccess(false);
            resultBean.setMessage("参数不为空");
        }
        logger.debug(resultBean.toString());
        return resultBean;
    }

    @GetMapping("logout")
    public ResultBean<String> logout(HttpSession session){
        ResultBean<String> resultBean = new ResultBean<>();
        session.removeAttribute(EwcrmConstants.SessionConst.SYS_USER);
        resultBean.setSuccess(true);
        return resultBean;
    }

}
